var flag = "";

/**
 * 重置按钮点击事件
 */
function reset(){
    document.getElementById("employAcount").value="";
    document.getElementById("employPwd").value="";
}

//请求对象
var req;
/**
 * 初始化加载数据
 */
function init(){
    req = new XMLHttpRequest();
    req.open("get","http://localhost:3000/empList",true);
    req.onreadystatechange=callback;
    req.send(null);
}
//回调函数
function callback(){
    if(req.readyState==4 && req.status==200){
        sessionStorage.setItem("empList",req.responseText); 
    }
}

/**
 * 登录按钮点击事件
 */
function doLogin(){
    //获取数据
    var loginAcount = document.getElementById("employAcount").value;
    var loginPwd = document.getElementById("employPwd").value;
    //遍历数据校验
    var empData = JSON.parse(sessionStorage.getItem("empList"));
    for(var i=0; i<empData.length; i++){
        if(empData[i].employAcount == loginAcount && empData[i].employPwd == loginPwd){
            createDivTS("登录成功！");
            flag = true;
            break;
        }
    }
    if(i == empData.length){
        createDivTS("登录失败！");
        flag = false;
    }
}

var divObj;
/**
 * 创建提示框
 */
function createDivTS(ts){
    divObj = document.createElement("div");
    divObj.innerText = ts;
    divObj.style.border="1px black solid";
    divObj.style.width="160px";
    divObj.style.lineHeight="30px";
    divObj.style.textAlign="center";
    divObj.style.backgroundColor = "white";
    divObj.style.position = "absolute";
    divObj.style.top = "100px";
    divObj.style.left = "50%";
    divObj.style.transform="translateX(-50%)";
    document.body.appendChild(divObj);
    window.setTimeout("clearDivTS()",1000);
}

/**
 * 消除提示框
 */
 function clearDivTS(){
    divObj.remove();
    if(flag){
        //切换模块
        document.getElementById("login").style.display="none";
        document.getElementById("select").style.display="block";
    }
    
}

/**
 * 查询数据
 */
function searchData(){
    //清除脏数据
    document.getElementById("tableData").innerText = "";
    //获取数据
    var tj = document.getElementById("tj").value;
    //遍历数据校验
    var empData = JSON.parse(sessionStorage.getItem("empList"));
    for(var i=0; i<empData.length; i++){
        if(empData[i].employAcount.indexOf(tj) != -1 || empData[i].employName.indexOf(tj) != -1){
            createTableNodes(empData[i]);
        }
    }
}

/**
 * 创建并渲染表格内数据
 */
 function createTableNodes(obj){
    //创建行
    var tr = document.createElement("tr");
    //创建单元格
    var td1 = document.createElement("td");
    td1.innerText = obj.employId;
    var td2 = document.createElement("td");
    td2.innerText = obj.employAcount;
    var td3 = document.createElement("td");
    td3.innerText = obj.employName;
    var td4 = document.createElement("td");
    td4.innerText = obj.employPwd;
    var td5 = document.createElement("td");
    td5.innerText = obj.employDate;
    //td添加到tr中
    tr.appendChild(td1);
    tr.appendChild(td2);
    tr.appendChild(td3);
    tr.appendChild(td4);
    tr.appendChild(td5);
    //tr添加到tbody中
    document.getElementById("tableData").appendChild(tr);
}